import { IAnswerChecker } from './IAnswerChecker'
import { IAnswer } from '../../Model/Answer'
import { ICheckResponse } from '../../Model/CheckResponse'
import { IQuestion } from '../../Model/Question'

export class GuessNumAnswerChecker implements IAnswerChecker {
  Check (question: IQuestion, anwer: IAnswer): ICheckResponse {
    let numA = 0
    let numB = 0

    for (let i = 0; i < 4; i++) {
      const indexOf = question.Question.indexOf(anwer.Answer[i])
      if (indexOf !== -1 && indexOf === i) {
        numA += 1
      } else if (indexOf !== -1) {
        numB += 1
      }
    }

    return {
      Message: numA + 'A' + numB + 'B',
      IsRight: numA === 4
    }
  }

  Validate (anwer: IAnswer): boolean {
    if (anwer.Answer.length > 4) {
      return false
    }
    for (let i = 0; i < 3; i++) {
      if (anwer.Answer.slice(i + 1).indexOf(anwer.Answer[i]) !== -1) {
        return false
      }
    }
    return true
  }
}
